/**
 * @program: clms-second
 * @description:
 * @author: mitu_echo
 * @create: 2021-07-24 20:21
 **/

package com.second.lease.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.second.common.pojo.R;
import com.second.lease.dao.CarTypeDao;
import com.second.lease.pojo.entity.CarType;
import com.second.lease.pojo.vo.CarTypeVO;
import com.second.lease.service.CarTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CarTypeServiceImpl implements CarTypeService {

    @Autowired
    private CarTypeDao carTypeDao;


    @Override
    public R showAllCarType(CarTypeVO carTypeVO) {

        if (carTypeVO.getPageno() != null) {
            PageHelper.startPage(carTypeVO.getPageno(),carTypeVO.getPagesize());
            QueryWrapper queryWrapper = new QueryWrapper();
            if (StringUtils.isNotEmpty(carTypeVO.getTypeName())) {
                queryWrapper.like("type_name",carTypeVO.getTypeName());
            }
            List carTypeList = carTypeDao.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(carTypeList);
            return R.success().putListData(carTypeList).putObject(pageInfo);
        } else {
            List<CarType> carTypeList = carTypeDao.selectList(null);
            return R.success().putListData(carTypeList);
        }

    }

    @Override
    public R updateCarType(CarType carType) {

        int i = carTypeDao.updateById(carType);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R deleteCarType(Integer id) {

        int i = carTypeDao.deleteById(id);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R addCarType(CarType carType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type_name",carType.getTypeName());
        CarType c = carTypeDao.selectOne(queryWrapper);
        if ( c == null) {
            // 写入当前时间
            carType.setAddtime(LocalDateTime.now().toString());
            int i = carTypeDao.insert(carType);
            if (i>0) {
                return R.success();
            }
            return R.error();
        }
        return R.error();
    }
}
